<?php
namespace app\index\controller;

use Config;
use Hook;
use Session;
use app\common\library\Auth;
use app\common\library\Jwt;
use think\exception\HttpResponseException;
use app\common\exception\System as SystemException;
use app\common\exception\User as UserException;
use app\common\controller\Common as CommonController;

class Common extends CommonController
{
	/**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];

    /**
     * 权限Auth
     * @var Auth
     */
    protected $auth = null;

	/**
	 * 初始化
	 */
	protected function initialize()
	{
		parent::initialize();

		$modulename     = $this->request->module();
		$controllername = strtolower($this->request->controller());
		$actionname     = strtolower($this->request->action());

        $this->auth = Auth::instance();

        // 通过JWT初始化用户
        $jwtToken = $this->request->header('Authorization');

        $token = NULL;

        if (!$jwtToken) {
            if (!$this->auth->match($this->noNeedLogin, $actionname)) {
                // 未登录

                $response = $this->errorReturn(UserException::E_NOT_LOGIN, UserException::$messageList[UserException::E_NOT_LOGIN]);
                throw new HttpResponseException($response);
            }
        } else {
            $token = Jwt::validateToken($jwtToken);

            if (!$token && !$this->auth->match($this->noNeedLogin, $actionname)) {
                // 已过期
                $response = $this->errorReturn(UserException::E_NOT_LOGIN, UserException::$messageList[UserException::E_NOT_LOGIN]);
                throw new HttpResponseException($response);
            }
        }

        $path = str_replace('.', '/', $controllername) . '/' . $actionname;
        // 设置当前请求的URI
        $this->auth->setRequestUri($path);
        // 检测是否需要验证登录
        if (!$this->auth->match($this->noNeedLogin, $actionname)) {
            //初始化
            $this->auth->init($token);
            //检测是否登录
            if (!$this->auth->isLogin()) {
                // 判断是否多设备登录

                $error = $this->auth->getError();
                if (!empty($error) && $error == UserException::getErrorMsg(UserException::E_OTHER_LOGIN)) {
                    $response = $this->errorReturn(UserException::E_OTHER_LOGIN, UserException::$messageList[UserException::E_OTHER_LOGIN]);
                    throw new HttpResponseException($response);
                } else {
                    $response = $this->errorReturn(UserException::E_NOT_LOGIN, UserException::$messageList[UserException::E_NOT_LOGIN]);
				    throw new HttpResponseException($response);
                }
            }

            // 判断是否需要验证权限
            if (!$this->auth->match($this->noNeedRight)) {
                // 判断控制器和方法判断是否有对应权限
                if (!$this->auth->check($path)) {
                    $response = $this->errorReturn(UserException::E_NO_PURVIEW, UserException::$messageList[UserException::E_NO_PURVIEW]);
					throw new HttpResponseException($response);
                }
            }
        } else {
            // 不需要登录,如果有传递token才验证是否登录状态
            if ($token) {
                $this->auth->init($token);
            }
        }
        // var_dump($this->auth->status);die;
        if(!is_null($this->auth->status) && $this->auth->status != model('common/Members')::STATUS_ENABLE){
            $response = $this->errorReturn(12345, '使用者狀態异常');
            throw new HttpResponseException($response);
        }
        // 检测单点登录
        // $loginToken = Session::get('login_token');
        // if ($this->auth->user_id && !empty($loginToken) && $this->auth->token != $loginToken) {
        //     $response = $this->errorReturn(UserException::E_IS_LOGIN, UserException::$messageList[UserException::E_IS_LOGIN]);
        //     throw new HttpResponseException($response);
        // }
	}
}
